package com.cwj.film.Listener;


import com.cwj.common.utils.utils;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

@Component
public class ServletContextListener implements ApplicationListener<WebServerInitializedEvent> {
    @Resource
    private ServletContext servletContext;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {

        int port = webServerInitializedEvent.getWebServer().getPort();
        String ipAddress = utils.getIpAddress();
        String applicationName = webServerInitializedEvent.getApplicationContext().getApplicationName();
        String html = "http://"+ipAddress + ":" + port + "/" + applicationName ;
        System.out.println(html);
        servletContext.setAttribute("FilmUrl",html);
        stringRedisTemplate.opsForValue().append("FilmUrl",html);

    }
}
